package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * @author 苏成博
 * @date 2023-02-08 10:46
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveDish(@RequestBody DishDTO dishDTO) {
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        dishService.saveDish(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除功能")
    public Result deleteDish(String ids) {
        dishService.deleteByIds(ids);
        //批量删除缓存中的键
        String key = "dish_*";
        cleanCache(key);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findById(@PathVariable Integer id) {
        DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新菜品接口")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        dishService.updateDish(dishDTO);
        String key = "dish_*";
        cleanCache(key);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Dish>> findByCategoryId(Integer categoryId,String name) {
        List<Dish> dishList=null;
        if (categoryId!=null){
            dishList=dishService.findByCategoryId(categoryId);
        }
        if (name!= null){
            dishList=dishService.findDishByName(name);
        }
        return Result.success(dishList);
    }
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        String key="dish_*";
        cleanCache(key);
        dishService.updateDishStatus(status,id);
        return Result.success();
    }
    private void cleanCache(Object key){
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }

}
